///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.share {
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	
	public class TextLink {
		private static var _log:ILogger;
		
		public static function Facebook(url:String,title:String):void {
			if (_log==null) _log = Log.getLogger("share.TextLink");
			
			var _title:String = parseTitle(title);
			var _uri:String = "http://www.facebook.com/sharer.php?u="+ url +"&t="+ _title;
			try {
				navigateToURL(new URLRequest(_uri),"_blank");
			} catch (e:Error) {
				_log.error("Unable to navigate to url!");
			}
		}
		
		public static function StumbleUpon(url:String,title:String):void {
			if (_log==null) _log = Log.getLogger("share.TextLink");
			
			var _title:String = parseTitle(title);
			var _uri:String = "http://www.stumbleupon.com/submit?url="+ url +"&title="+ _title;
			try {
				navigateToURL(new URLRequest(_uri),"_blank");
			} catch (e:Error) {
				_log.error("Unable to navigate to url!");
			}
		}
		
		public static function Digg(url:String,title:String):void {
			if (_log==null) _log = Log.getLogger("share.TextLink");
			
			var _title:String = parseTitle(title);
			var _uri:String = "http://digg.com/submit/?phase=2&url="+ url +"&title="+ _title;
			try {
				navigateToURL(new URLRequest(_uri),"_blank");
			} catch (e:Error) {
				_log.error("Unable to navigate to url!");
			}
		}
		
		public static function Reddit(url:String):void {
			if (_log==null) _log = Log.getLogger("share.TextLink");
			
			var _uri:String = "http://www.reddit.com/submit?url="+ url;
			try {
				navigateToURL(new URLRequest(_uri),"_blank");
			} catch (e:Error) {
				_log.error("Unable to navigate to url!");
			}
		}
		
		public static function DelIcioUs(url:String,title:String):void {
			if (_log==null) _log = Log.getLogger("share.TextLink");
			
			var _title:String = parseTitle(title);
			var _uri:String = "http://delicious.com/save?jump=yes&url="+ url +"&title="+ _title;
			try {
				navigateToURL(new URLRequest(_uri),"_blank");
			} catch (e:Error) {
				_log.error("Unable to navigate to url!");
			}
		}
		
		public static function Twitter(tweet:String,params:Object=null):void {
			if (_log==null) _log = Log.getLogger("share.TextLink");
			
			var _uri:String = "http://twitter.com/share?text="+escape(tweet);
			if (params) {
				_uri += "&";
				for (var i:String in params) {
					_uri += i + "=" + escape(params[i]) + "&";
				}
				_uri = _uri.substr(0,-1);
			}
			try {
				navigateToURL(new URLRequest(_uri),"_blank");
			} catch (e:Error) {
				_log.error("Unable to navigate to url!");
			}
		}
		
		public static function MySpace(url:String,title:String,content:String="",location:uint=3):void {
			if (_log==null) _log = Log.getLogger("share.TextLink");
			
			var _params:String = "t="+escape(title)+"&c="+content+"&u="+escape(url)+"&l="+location;
			var _uri:String = "http://www.myspace.com/Modules/PostTo/Pages/?"+_params;
			try {
				navigateToURL(new URLRequest(_uri),"_blank");
			} catch (e:Error) {
				_log.error("Unable to navigate to url!");
			}
		}
		
		private static function parseTitle(title:String):String {
			var _pattern:RegExp = / /g;
			return title.replace(_pattern,"+");
		}
	}	
}